package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author yjj
 * @date 2021年09月19日 15:51
 */
@Api("数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
@CrossOrigin // 解决跨域
public class DictController {
    @Autowired
    private DictService dictService;

    //根据数据Id查询子数据
    @ApiOperation(value = "数据字典查询")
    @GetMapping("/findChildrenData/{id}")
    public Result cmnList(@PathVariable Long id) {
        List<Dict> result = dictService.findChildrenData(id);
        return Result.ok(result);
    }

    //导出数据字典数据
    @RequestMapping("/exportData")
    public void exportDict(HttpServletResponse response) {
        dictService.exprotDict(response);
    }

    //导入数据字典
    @RequestMapping("/importData")
    public Result importData(MultipartFile file) {
        dictService.importData(file);
        return Result.ok();
    }

    //根据dictcode和value值查询
    @GetMapping("/getName/{dictcode}/{value}")
    public String getName(@PathVariable String dictcode,
                          @PathVariable String value) {
        String name = dictService.getDictName(dictcode, value);
        return name;
    }
    //根据value值查询
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable String value) {
        String name = dictService.getDictName(null, value);
        return name;
    }

    //根据dictCode获取下级节点
    @ApiOperation("根据dictCode获取下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode( @ApiParam(name = "dictCode", value = "节点编码", required = true)
                                      @PathVariable String dictCode){
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }



}
